<template>
	<div class="table">
		<view class="table-th-row">
			<view class="table-th td" v-for="th in tableTh" :key="th['key']" :style="{width:th['w']}">{{th['name']}}</view>
		</view>
		<!-- <scroll-view 
		style="height: 200px;" 
		class="table-body"
		 :show-scrollbar="true" 
		 :scroll-top="scrollTop" 
		 :scroll-y="true">
			
			<div class="table-row" v-for="(row, i) in listAr" :key="i" @touchstart="touchstart"  @touchend="touchend">
				<view class="td" v-for="th in tableTh" :key="th['key']" :style="{width:th['w']}">
					{{row[th['key']]}}{{row}}
				</view>
			</div>
		</scroll-view> -->
		
		<view class="table-body" :style="{maxHeight:scrollHeight+'px'}">
			<view :style="{transform:'translateY(-'+scrollTop+'px)'}"  @touchstart="touchstart"  @touchend="touchend">
				<view class="table-row" v-for="(row, i) in listAr" :key="i">
					<view class="td" v-for="th in tableTh" :key="th['key']" :style="{width:th['w']}">
						{{row[th['key']]}} 11
					</view>
				</view>
			</view>
		</view>
		
	</div>
</template>

<script>
	/*
	listAr  循环数组
	scrollHeight  scroll可视区域高度
	scrollTimer  是否默认开始滚动
	*/
	export default {
		name: "WF-scroll",
		props: {
		},
		data() {
			return {
				scrollHeight: 140,
				tableTh:[
					{name:"序号",w:"60px",key:'1'},
					{name:"产品编码",w:"100px",key:'2'},
					{name:"物料编码",w:"100px",key:'3'},
					{name:"物料名称",w:"100px",key:'4'},
					{name:"物料规格",w:"100px",key:'5'},
					{name:"业务状态",w:"100px",key:'6'},
					{name:"生产数量",w:"100px",key:'7'},
					{name:"已齐套数量",w:"100px",key:'8'},
				],
				listAr:[1,2,3,4,5,3,4,5,3,4,5],
				tableRowH:[],
				scrollTop: 0, //判断滚动所用为防止滚动到底部多次赋值
				touchstartType: false,
			};
		},
		created() {
			setTimeout(() => {
				
				
				const query = uni.createSelectorQuery().in(this);
				query.selectAll('.table-row').boundingClientRect(data => {
					this.tableRowH = data.map(d=>d.height);
					
					let countH = this.tableRowH.reduce((a,b)=>a+b,0);
					console.log(countH)
					
					if(countH>this.scrollHeight){
						// this.tableRowH = [...this.tableRowH,...this.tableRowH]
						// this.listAr = [...this.listAr,...this.listAr]
						this.setTimer()
					}
					
					
				}).exec();
			}, 5000)
		},
		beforeDestroy() {
			if(this.timer){
				clearInterval(this.timer);
				this.timer = null;
			}
		},
		methods: {
			touchstart() {
				this.touchstartType = true
			},
			touchend() {
				this.touchstartType = false
			},
			// 这里设置滚动速度时间越短滚动则越快
			setTimer() {
				this.timer = setInterval(() => {
					if(this.touchstartType)return;
					if(this.scrollTop>this.tableRowH[0]){
						this.scrollTop = 0
						let item = this.listAr.splice(0,1);
						this.listAr.push(item[0])
						
						let tableRowH = this.tableRowH.splice(0,1);
						this.tableRowH.push(tableRowH[0])
					}
					this.scrollTop++
				}, 5000/24)
			},
		}
	}
</script>

<style scoped lang="scss">
	.table{
		overflow: hidden;
		overflow-x: auto;
		border: 1px solid #ddd;
	}
	
	.td{
		flex-shrink:0;
		padding: 10rpx 16rpx;
		border-bottom: 1px solid #ddd;
		border-right: 1px solid #ddd;
		font-size: 24rpx;
	}
	
	.table-th-row{
		display: flex;
		
		z-index: 10;
		position: relative;
		.table-th{
			background: #d9fbdb;
			color: #5cc188;
			font-weight: bold;
		}
		
	}
	.table-body{
		
		.table-row{
			display: flex;
		}
	}
</style>
